﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WeChatParking.Core.BLL
{
    public class LoginUser
    {
        /// <summary>
        /// 验证用户名密码企业
        /// </summary>
        /// <param name="enterprise"></param>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public Entity.User_info ValidUser(string enterprise, string username, string password)
        {

            using (var dbContext = Repository.DbContext.Create())
            {
                var precidate = new { EnterpriseCode = enterprise, UserName = username };
                var user = dbContext.GetList<Entity.User_info>(precidate, null).FirstOrDefault();
                if (user == null)
                    return null;
                if (this.ValidPassword(user.UserCode, password, user.UserLogPwd))
                    return user;
            }
            return null;
        }
        /// <summary>
        /// 验证用户名密码企业
        /// </summary>
        /// <param name="enterprise"></param>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public Entity.User_info ValidUser(string UserCode, string Pwd)
        {

            using (var dbContext = Repository.DbContext.Create())
            {
                var precidate = new { UserCode = UserCode, UserLogPwd = Pwd };
                var user = dbContext.GetList<Entity.User_info>(precidate, null).FirstOrDefault();
                if (user == null)
                    return null;
                return user;
            }
        }

        /// <summary>
        /// 校验密码
        /// </summary>
        /// <param name="account">用户账号</param>
        /// <param name="password">明文密码</param>
        /// <param name="md5pass">加密后的密码</param>
        /// <returns></returns>
        private bool ValidPassword(string account, string password, string md5pass)
        {
            string md5 = this.EncodePasswod(account, password);
            return string.Equals(md5, md5pass);
        }
        /// <summary>
        /// 对密码进行加密
        /// </summary>
        /// <param name="account"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public string EncodePasswod(string account, string password)
        {
            return Util.Security.GetMd5Hash(account + password).ToUpper();
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <param name="oldpass">原密码</param>
        /// <param name="newpass">新密码</param>
        /// <returns></returns>
        public string ChangePassword(string UserCode, string oldpass, string newpass)
        {
            string errMsg = "更新失败";

            var user = this.ValidUser(UserCode, this.EncodePasswod(UserCode, oldpass));
            if (user != null)
            {
                Core.BLL.User_info bll = new Core.BLL.User_info();
                string newMd5Pass = this.EncodePasswod(user.UserCode, newpass);
                user.UserLogPwd = newMd5Pass;
                return bll.Update(user);
            }
            else
                errMsg = "用户密码验证失败";
            return errMsg;
        }
    }
}
